package com.quectel.core.module.ai.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.ai.dao.AiLibApplicationDao;
import com.quectel.core.module.ai.dto.AiLibApplicationDto;
import com.quectel.core.module.ai.entity.AiLibApplicationEntity;
import com.quectel.core.module.ai.service.AiLibApplicationService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-24 19:15:09
 */
@DubboService
public class AiLibApplicationServiceImpl implements AiLibApplicationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AiLibApplicationServiceImpl.class);

    private static final String AI_LIB_APPLICATION_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "AI_LIB_APPLICATION_CACHE_BY_ID:";

    @Autowired
    private AiLibApplicationDao aiLibApplicationDao;


    @Override
    public AiLibApplicationDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                AI_LIB_APPLICATION_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public AiLibApplicationDto selectById(Long id) {
        AiLibApplicationEntity entity = aiLibApplicationDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, AiLibApplicationDto.class));
    }

    @Override
    public List<AiLibApplicationDto> queryList(Map<String, Object> params) {
        List<AiLibApplicationEntity> list = aiLibApplicationDao.queryList(params);
        List<AiLibApplicationDto> result = CopyUtils.copyList(list, AiLibApplicationDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private AiLibApplicationDto paddingField(AiLibApplicationDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return aiLibApplicationDao.queryTotal(params);
    }

    @Override
    public Long save(AiLibApplicationDto dto) {
        AiLibApplicationEntity entity = CopyUtils.copyObj(dto, AiLibApplicationEntity.class);
        entity.setId(Snowflake.nextId());
        aiLibApplicationDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AiLibApplicationDto dto) {
        AiLibApplicationEntity entity = CopyUtils.copyObj(dto, AiLibApplicationEntity.class);
        aiLibApplicationDao.updateById(entity);
        CacheKit.invalidRedisCache(AI_LIB_APPLICATION_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(AiLibApplicationDto dto) {
        AiLibApplicationEntity entity = CopyUtils.copyObj(dto, AiLibApplicationEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, aiLibApplicationDao);

        CacheKit.invalidRedisCache(AI_LIB_APPLICATION_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        aiLibApplicationDao.deleteById(id);

        CacheKit.invalidRedisCache(AI_LIB_APPLICATION_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
